<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="name" label="类别名称" required>
				<uni-easyinput placeholder="类别名称" v-model="formData.name" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="description" label="跳转页面">
				<uni-easyinput placeholder="点击后跳转到的页面" v-model="formData.description" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="选择页面">
				<uni-data-select v-model="value" :localdata="range"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item v-if="value == '/pages/product/product?id='" label="选择商品">
				<uni-data-select v-model="values" :localdata="goodsList" placeholder="商品详情页才需要选择"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item v-if="value == '/pages/product/list?id='" label="选择分类">
				<uni-data-select v-model="values" :localdata="categoriesList"
					placeholder="商品分类页才需要选择"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="icon" label="图标地址" required>
				<cloud-image placeholder="类别图标地址" v-model="formData.icon"></cloud-image>
			</uni-forms-item>
			<uni-forms-item name="sort" label="排序">
				<uni-easyinput placeholder="类别排序，越大越靠后" type="number" v-model="formData.sort"></uni-easyinput>
			</uni-forms-item>

			<uni-forms-item name="is_index_show" label="首页显示">
				<switch @change="binddata('is_index_show', $event.detail.value)" :checked="formData.is_index_show">
				</switch>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/jlg-homepage-cetagories.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'jlg-homepage-cetagories';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"name": "",
				"icon": "",
				"sort": null,
				"description": "",
				"is_index_show": true
			}
			return {
				formData,
				formOptions: {},
				value: '',
				values: '',
				goodsList: [],
				categoriesList: [],
				range: [{
						value: '/pages/product/product?id=',
						text: "商品详情页"
					},
					{
						value: "/pages/product/list?id=",
						text: "商品分类页"
					},
					{
						value: "/pages/coupon/coupon",
						text: "领卷中心"
					}
				],
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		onLoad() {
			this.goodsData();
			this.categoriesData();
		},
		watch: {
			value(val) {
				this.formData.description = val
			},
			values(val) {
				this.formData.description = this.value + val
			}
		},
		methods: {
			goodsData() {
				db.collection('opendb-mall-goods').where({
					is_on_sale: true
				}).field('_id as value, name as text,is_on_sale').get().then((res) => {
					this.goodsList = res.result.data
				})
			},
			categoriesData() {
				db.collection('opendb-mall-categories').where({
					is_index_show: true
				}).field('_id as value, name as text,is_index_show').get().then((res) => {
					this.categoriesList = res.result.data
				})
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>